package eclipse;

import java.util.Arrays;
import java.util.Random;

/*
并归算法
 */
public class ArrayExercise07 {
    public static void main(String[] args) {
        int[] arr1 = createMyArray();
        int[] arr2 = createMyArray();
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        int[] arr3 = new int[arr1.length + arr2.length];
        System.out.println("--------------------------");
        mySort(arr1, arr2, arr3);
        System.out.println("--------------------------");
        System.out.println(Arrays.toString(arr3));
    }

    private static int[] mySort(int[] arr1, int[] arr2, int[] arr3) {
        int i;
        int j = 0; // arr1
        int k = 0; // arr2
        boolean flag = false;
        for(i = 0; i < arr3.length; i++) {
            if(j >= arr1.length){
                arr3[i] = arr2[k++];
                continue;
            }
            if(k >= arr2.length){
                arr3[i] = arr1[j++];
                continue;
            }

            if (arr1[j] < arr2[k]) {
                arr3[i] = arr1[j++];
            }else{
                arr3[i] = arr2[k++];
            }
        }
        return arr1;

    }
    private static int[] createMyArray() {
        int len = 5 + new Random().nextInt(6);
        int[] arr = new int[len];
        for(int i = 0; i < arr.length; i++) {
            arr[i] = new Random().nextInt(100);
        }
        Arrays.sort(arr);
        return arr;
    }
}
